//
//  CitysModel.m
//  LzzizDemo
//
//  Created by xbnzd on 16/11/8.
//  Copyright © 2016年 xbnzd. All rights reserved.
//

#import "CitysModel.h"
#import "GVUserDefaults+LZUserLocationInfo.h"

@implementation CitysModel

#pragma mark - 实现NSCoding协议的方法

-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.enName = [aDecoder decodeObjectForKey:@"enName"];
        self.arName = [aDecoder decodeObjectForKey:@"arName"];
        self.chName = [aDecoder decodeObjectForKey:@"chName"];
        self.cityNO = [aDecoder decodeObjectForKey:@"cityNO"];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.enName forKey:@"enName"];
    [aCoder encodeObject:self.arName forKey:@"arName"];
    [aCoder encodeObject:self.chName forKey:@"chName"];
    [aCoder encodeObject:self.cityNO forKey:@"cityNO"];
}

- (NSString*)name {
    if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
        return self.arName;
    }
    else {
        return self.enName;
    }
}

#pragma mark - 存城市信息

+(void)setCityMessageWithCityEntity:(CitysModel*)city
{
    if (city == nil) {
        return;
    }
    // 序列化
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:city];
    [GVUserDefaults standardUserDefaults].cityModel = myEncodedObject;
}

#pragma mark - 获取存下的城市信息

+(CitysModel*)getCityModel
{
    // 反序列化
    NSData *myEncodedObject = [GVUserDefaults standardUserDefaults].cityModel;
    if (myEncodedObject) {
        CitysModel *city = (CitysModel *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
        return city;
    }
    
    return nil;
}

@end
